<?php
/**
 *
 * (model)
 */
namespace App\Models\SalesReport;

use App\Models\Basics\SysCountry;
use App\Models\Basics\SysArea;
use App\Models\Basics\SysICountry;
use App\Models\Basics\SysPlat;
use App\Models\Product\ProductClassify;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class MonthlyCTarget extends Model
{
    protected $table = 'sr_monthly_c_target';

    protected $guarded = [];

    public $timestamps = false;

    const E_NUM = 18;

    const START_DATE_LAST = '2021-09-01';//已保存的数据的起始日期

    const START_DATE = '2023-09-01';

    const END_DATE = '2025-02-01';

    public function classify(){
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public static function getData($re){
        $area = [];
        $seller = [];
        foreach ($re as $row){
            $temp = [];
            if(!array_key_exists($row->country_id,$area)){
                $areaId = SysICountry::getAreaIdByCountryId($row->country_id);
                $area[$row->country_id] = SysArea::getSysAreaById($areaId);
            }
            $temp['plat'] = SysPlat::$_platformType[$row->plat_id] ?? '';
            $temp['area'] = $area[$row->country_id];
            $temp['country'] = $row->country->country ?? '';
            $temp['season'] = ProductClassify::$_season[$row->classify->season ?? 3];
            $temp['classify'] = $row->classify->title ?? '';
            $key = $row->country_id.'_'.$row->classify_id;
            if(!array_key_exists($key,$seller)){
                $sellerId = MonthlyCv::query()
                    ->where('country_id',$row->country_id)
                    ->where('classify_id',$row->classify_id)
                    ->value('seller_id');
                $seller[$key] = User::getUsernameById($sellerId);
            }
            $temp['seller_id'] = $seller[$key];
            for($i = 0; $i < self::E_NUM; $i++){
                $temp['estimate_volume'.$i] = $row->{'estimate_volume'.$i};
            }
            yield $temp;
        }
    }
}
